package com.gis.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.gis.bean.GoodsType;
import com.gis.util.JDBCUtils;

import cn.hutool.core.collection.CollectionUtil;

public class GoodsTypeDao {
	QueryRunner qr = new QueryRunner(JDBCUtils.getC3p0DateSource());
	/**
	 * 获取所有的物品类型
	 * @return GoodsType list
	 */
	public List<GoodsType> listAll() {
		List<GoodsType> goodsTypes = new ArrayList<GoodsType>();
		try {
			goodsTypes = qr.query("select id, name from tb_goods_type", new BeanListHandler<GoodsType>(GoodsType.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return goodsTypes;
	}
	
	/**
	 * 根据物品类型id, 获取对应的名称
	 * @param goodsTypeId
	 * @return
	 */
	public String getTypeById(int goodsTypeId) {
		List<Map<String, Object>> goodsTypes = new ArrayList<Map<String, Object>>();
		try {
			goodsTypes = qr.query("select name from tb_goods_type where id = ?", new MapListHandler(), goodsTypeId);
			if(CollectionUtil.isEmpty(goodsTypes)) {
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (String)goodsTypes.get(0).get("name");
	}

}
